package com.xiaobai.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

@Component
public class WebServiceConfig implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        /*使用对应工厂类提供的接口,定制化内嵌tomcat的connector*/
        ((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol http11NioProtocol=(Http11NioProtocol)connector.getProtocolHandler();

                /*定制化KeepLive的失效时间(多久没请求则失效)*/
                http11NioProtocol.setKeepAliveTimeout(3);/*30秒*/
                /*当客户端发送了超过了1个请求则自动断开KeepLive长连接*/
                http11NioProtocol.setMaxKeepAliveRequests(10);
//                http11NioProtocol.set

                /*还可以修改很多......*/

            }
        });
    }

}
